/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nttnetworks.com.controls.questions;

import java.util.ArrayList;

/**
 *
 * @author minh
 */
public class questionPage extends javax.swing.JPanel {

    private ArrayList<questionAnsw> items = new ArrayList<>();
    
    /**
     * Creates new form questions
     */
    public questionPage() {
        initComponents();
    }
    
    public int length() {
        return items.size();
    }
    
    public questionAnsw get(int index) {
        return items.get(index);
    }
    
    public void fixHeight() {
        for (int i=0; i<items.size(); i++) {
            items.get(i).setSize(questionPage.this.getWidth(), 0);
            items.get(i).fixHeight();
            
            if (i > 0) {
                questionAnsw p = items.get(i-1);
                items.get(i).setLocation(0, p.getY()+p.getHeight()+5);
            } else {
                items.get(i).setLocation(0, 5);
            }
        }

        if (items.size() > 0) {
            questionAnsw p = items.get(items.size()-1);
            questionPage.this.setSize(questionPage.this.getWidth(), p.getY()+p.getHeight()+5);
        }
    }

    public void addQuestion(String question, String[] answers, int type, int num) {
        questionAnsw item = new questionAnsw();
        int y = 5;
        
        if (items.size() > 0) {
            questionAnsw litem = items.get(items.size()-1);
            y = litem.getHeight()+litem.getY()+5;
        }
        
        item.showQuestion(question, type, answers, num);
        item.setLocation(0, y);
        item.setSize(this.getWidth(), 10);
        
        items.add(item);
        this.add(item);
        fixHeight();
    }
    
    public void clear() {
        for (int i=0; i<items.size(); i++) {
            this.remove(items.get(i));
        }
        items.clear();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(null);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
